package mock

import (
	"gopkg.in/h2non/gock.v1"
	"pd/server/com"
)

type HttpMapItem struct {
	repFilePath string
	host string
	path string
	repCode int
	repBody string
	gockRequest *gock.Request
}

func (this *HttpMapItem) ResponseFilePath(repFilePath string) *HttpMapItem {
	this.repFilePath = repFilePath
	return this
}

func (this *HttpMapItem) HttpHost(host string) *HttpMapItem {
	this.host = host
	return this
}

func (this *HttpMapItem) HttpPath(path string) *HttpMapItem {
	this.path = path
	return this
}

func (this *HttpMapItem) ResponseCode(repCode int) *HttpMapItem {
	this.repCode = repCode
	return this
}

func (this *HttpMapItem) ResponseBody(repBody string) *HttpMapItem {
	this.repBody = repBody
	return this
}

func (this *HttpMapItem) Build() *gock.Request {
	gock.EnableNetworking()

	if this.path == "" {
		this.path = "/"
	}

	if this.repCode == 0 {
		this.repCode = 200
	}

	// this.gockRequest = gock.New(this.host).Get(this.path).Persist()
	this.gockRequest = gock.New(this.host).Persist()
	this.gockRequest.Reply(this.repCode)

	if this.repFilePath != "" {
		this.gockRequest.Response.File(com.ExpandedFilename(this.repFilePath))
	} else {
		this.gockRequest.Response.BodyString(this.repBody)
	}

	return this.gockRequest
}

func AddHttpMap() *HttpMapItem {
	httpMapItem := &HttpMapItem{}

	return httpMapItem
}
